package com.tdkj.action.common;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;
import net.sf.json.util.CycleDetectionStrategy;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.tdkj.action.BaseAction;
import com.tdkj.model.information.BeanBaseInfo;
import com.tdkj.model.information.BeanInfoModule;
import com.tdkj.model.information.BeanModuleColumn;
import com.tdkj.model.user.BeanUser;
import com.tdkj.service.information.IBeanBaseInfoService;
import com.tdkj.service.information.IBeanInfoModuleService;
import com.tdkj.util.JsonUtil;

public class InfoCenterAction  extends BaseAction{
	private long id;
	private String classTypeJSON;
	private String modulename;
	private String moduletype;
	private String columnname;
	private String columntype;
	private String title;
	private String resource;
	private String content;
	private Map<String,String>prop=new HashMap<String,String>();
	private BeanInfoModule infoModule;
	private List<BeanInfoModule> infoModules=new ArrayList<BeanInfoModule>();
	private List<BeanModuleColumn> columns=new ArrayList<BeanModuleColumn>();
	private IBeanInfoModuleService infoModuleService;
	private IBeanBaseInfoService baseInfoService;
	 
	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getResource() {
		return resource;
	}

	public void setResource(String resource) {
		this.resource = resource;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getClassTypeJSON() {
		return classTypeJSON;
	}

	public BeanInfoModule getInfoModule() {
		return infoModule;
	}

	public void setInfoModule(BeanInfoModule infoModule) {
		this.infoModule = infoModule;
	}

	public void setClassTypeJSON(String classTypeJSON) {
		this.classTypeJSON = classTypeJSON;
	}

	public List<BeanInfoModule> getInfoModules() {
		return infoModules;
	}

	public void setInfoModules(List<BeanInfoModule> infoModules) {
		this.infoModules = infoModules;
	}

	public List<BeanModuleColumn> getColumns() {
		return columns;
	}

	public void setColumns(List<BeanModuleColumn> columns) {
		this.columns = columns;
	}

	public String getModulename() {
		return modulename;
	}
	public String getModuletype() {
		return moduletype;
	}

	public void setModuletype(String moduletype) {
		this.moduletype = moduletype;
	}

	public void setModulename(String modulename) {
		this.modulename = modulename;
	}
	public String getColumnname() {
		return columnname;
	}

	public void setColumnname(String columnname) {
		this.columnname = columnname;
	}

	public String getColumntype() {
		return columntype;
	}

	public void setColumntype(String columntype) {
		this.columntype = columntype;
	}

	public IBeanInfoModuleService getInfoModuleService() {
		return infoModuleService;
	}

	@Resource
	public void setInfoModuleService(IBeanInfoModuleService infoModuleService) {
		this.infoModuleService = infoModuleService;
	}
	
	public IBeanBaseInfoService getBaseInfoService() {
		return baseInfoService;
	}

	@Resource
	public void setBaseInfoService(IBeanBaseInfoService baseInfoService) {
		this.baseInfoService = baseInfoService;
	}
	public Map<String, String> getProp() {
		return prop;
	}

	public void setProp(Map<String, String> prop) {
		this.prop = prop;
	}
	
	//获取编辑列表
	public String findEditPage(){
		BeanBaseInfo bean=baseInfoService.findById(this.getId(), BeanBaseInfo.class);
		this.setTitle(bean.getTitle());
		this.setResource(bean.getResource());
		this.setModulename(bean.getModulename());
		this.setModuletype(bean.getModulename());
		moduletype=bean.getModuletype();
		columntype=bean.getColumntype();
		this.setColumnname(bean.getColumnname());
		this.setColumntype(bean.getColumntype());
		this.setContent(bean.getContent());
		this.setProp(bean.getProp());
		BeanInfoModule m=new BeanInfoModule();
		m.setModuletype(this.getModuletype());
		List<BeanInfoModule> ml=infoModuleService.findByExample(m);
		if(ml.size()==1) infoModule=ml.get(0);
		return findPageByMode2(bean.getModuletype(),bean.getColumntype())+"_edit";
	}
	public String editInfo(){
		BeanBaseInfo info=baseInfoService.findById(this.getId(), BeanBaseInfo.class);
		info.setTitle(this.getTitle());
		info.setContent(this.getContent());
		info.setModulename(this.getModulename());
		info.setModuletype(this.getModuletype());
		moduletype=this.getModuletype();
		info.setColumnname(this.getColumnname());
		info.setColumntype(this.getColumntype());
		info.setResource(this.getResource());
		info.setCreatetime(new Timestamp(System.currentTimeMillis()));
		if(this.getProp()!=null){
			info.setProp(this.getProp());
		}
		BeanUser u=this.getCurrentUser();
		info.setUser(u);
		baseInfoService.save(info);
		return findList();
	}
	//删除信息
	public String deleteInfo(){
		baseInfoService.deleteBaseInfo(this.getId());
		moduletype=this.getModuletype();
		return findList();
	}
	//根据模块和栏目获得信息列表
	public String findList(){
		String mt=this.getModuletype();
		String ct=this.getColumntype();
		this.setModuletype(mt);
		if(mt!=null&&ct!=null){
			Criteria criteria = this.createCriteria(BeanBaseInfo.class)
			.add(Restrictions.eq("moduletype", mt))
			.add(Restrictions.eq("columntype", ct))
			.addOrder(Order.desc("createtime"));// 对状态进行排序
			this.queryByPage(criteria, "infoCenterAction!findList.action", 10);
		}else{
			Criteria criteria = this.createCriteria(BeanBaseInfo.class)
			.add(Restrictions.eq("moduletype", mt))
			.addOrder(Order.desc("createtime"));// 对状态进行排序
			this.queryByPage(criteria, "infoCenterAction!findList.action", 10);
		}
		return "list";
	}
	
	//获取添加信息的界面,分为模式一和模式二
	public String findAddPage(){
		String mt=this.getModuletype();
		String ct=this.getColumntype();
		if(mt!=null&&ct!=null){
			return findPageByMode2(mt,ct)+"_add";
		}else{
			return findPageByMode1(mt)+"_add";
		}
	}
	
	//根据模式一获取地址（moduletype）
	private String findPageByMode1(String mt){
		String location="page";
		BeanInfoModule m=new BeanInfoModule();
		m.setModuletype(mt);
		List<BeanInfoModule> ml=infoModuleService.findByExample(m);
		if(ml.size()==1){
			infoModule=ml.get(0);
		} 
		if(mt.equals("1")){location="1page";}
		if(mt.equals("3")){location="3page";}
		if(mt.equals("7")){location="7page";}
		if(mt.equals("9")){location="9page";}
		return location;
	}
	//根据模式一获取地址（moduletype,columntype）
	private String findPageByMode2(String mt,String ct){
		String location="page";
		BeanModuleColumn bean=infoModuleService.findModuleColumn(mt,ct);
		this.setColumnname(bean.getColumnname());
		this.setColumntype(bean.getColumntype());
		this.setModulename(bean.getInforModule().getModulename());
		this.setModuletype(bean.getInforModule().getModuletype());
		BeanInfoModule m=new BeanInfoModule();
		m.setModuletype(this.getModuletype());
		List<BeanInfoModule> ml=infoModuleService.findByExample(m);
		if(ml.size()==1) infoModule=ml.get(0);
		if(mt.equals("1")){location="1page";}
		if(mt.equals("3")){if(ct.equals("3")||ct.equals("4")){location="334page";}}
		if(mt.equals("7")){location="7page";}
		if(mt.equals("9")){
			if(ct.equals("1")){location="91page";}
			if(ct.equals("2")){location="92page";}
		}
		return location;
	}
	
	//根据模块信息，获取添加的模块信息
	private String getLocation(){
		String location="page";
		String mType=this.getModuletype();
		String cType=this.getColumntype();
		String type=mType+"-"+cType;
		if(type.equals("1-1")||type.equals("1-2")){
			location="1page";;
		}
		if(mType.equals("3")){
			if(cType.equals("5")||cType.equals("6")){
				location="3-34page";
			}
		}
		if(mType.equals("7")){
			location="7page";
		}
		if(mType.equals("9")){
			if(cType.equals("1")){
				location="9-1page";
			}else if(cType.equals("2")){
				location="9-2page";
			}
		}
		return location;
	}
	//发布消息
	public String addInfo(){
		BeanBaseInfo info=new BeanBaseInfo();
		info.setTitle(this.getTitle());
		info.setContent(this.getContent());
		info.setModulename(this.getModulename());
		info.setModuletype(this.getModuletype());
		info.setColumnname(this.getColumnname());
		info.setColumntype(this.getColumntype());
		info.setResource(this.getResource());
		info.setCreatetime(new Timestamp(System.currentTimeMillis()));
		info.setClicktime(1);
		info.setStatus("未审核");
		if(this.getProp()!=null){
			info.setProp(this.getProp());
		}
		//BeanUser u= (BeanUser) ActionContext.getContext().getSession().get(PublicConfing.MEMBER_MEMBWE_OBJ_STRING_CONFIG);
		BeanUser u=baseInfoService.findById(1l, BeanUser.class);
		info.setUser(u);
		baseInfoService.save(info);
		return getLocation();
	}
}
